package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.MemCardDto;
import edu.suda.xsap.entity.MemCard;
import edu.suda.xsap.entity.Member;
import edu.suda.xsap.service.MemCardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/memcard")
@Slf4j
@Tags({@Tag(name = "会员卡管理", description = "会员卡管理")})
public class MemCardController {

    @Autowired
    private MemCardService memCardService;
    @Operation(summary = "查询会员卡",description = "查询所有的会员卡信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })

    @GetMapping("/list")
    public Result list() {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<MemCardDto> memCardDtos = memCardService.findAll();
            //
            return Result.success(memCardDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @Operation(summary = "添加会员卡",description = "会员卡")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "删除失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @PostMapping("/save")
    public Result createMemCard(@RequestBody MemCardDto memCardDto) {
        MemCard memCard = MemCard.builder()
                .name(memCardDto.getMemberCardName())
                .price(memCardDto.getPrice())
                .totalday(memCardDto.getAvailableDays())
                .totalcount(memCardDto.getAvailableTimes())
                .type(memCardDto.getType())
                .description(memCardDto.getDescription())
                .note(memCardDto.getNote())
                .build();
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            System.out.println(memCard);
            memCardService.save(memCard);//保存数据到数据库中
            return Result.success();
        } catch (Exception e) {
            log.error("保存失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @Operation(summary = "删除会员卡",description = "通过id删除会员卡")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "删除失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {//按id删除
        try {
            memCardService.deleteById(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }

    @Operation(summary = "修改会员卡",description = "修改会员卡信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "修改失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @PutMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result update(@RequestBody MemCard memCard) {//修改
        try {
            memCardService.update(memCard);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


}
